package com.utils;

import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;

public class TimeUtil {

    public static String getLasetTime(long lasetTime){
        // 计算剩余的天、小时、分钟和秒
        long days = TimeUnit.MILLISECONDS.toDays(lasetTime);
        lasetTime -= TimeUnit.DAYS.toMillis(days);

        long hours = TimeUnit.MILLISECONDS.toHours(lasetTime);
        lasetTime -= TimeUnit.HOURS.toMillis(hours);

        long minutes = TimeUnit.MILLISECONDS.toMinutes(lasetTime);
        lasetTime -= TimeUnit.MINUTES.toMillis(minutes);

        long seconds = TimeUnit.MILLISECONDS.toSeconds(lasetTime);

        // 格式化输出
        return String.format("%d天 %d小时 %d分钟 %d秒", days, hours, minutes, seconds);
    }

    public static String getLasetEndTime(LocalDateTime localDateTime){
        LocalDateTime now =LocalDateTime.now();

        if(now.isAfter(localDateTime)){
            return "";
        }

        // 计算两个时间点的差异
        Duration duration = Duration.between(now,localDateTime);

        // 获取差异的小时、分钟、秒
        long hours = duration.toHours();
        long minutes = duration.toMinutes() % 60;
        long seconds = duration.getSeconds() % 60;
        if(hours>24){
            long dayhours=hours;
            hours =hours%24;
            long days = dayhours/24;
            return days+"天"+hours + " 小时 " + minutes + " 分钟 " + seconds + " 秒";
        }
        return  hours + " 小时 " + minutes + " 分钟 " + seconds + " 秒";
    }

    public static String getLasetTime(LocalDateTime localDateTime){
        LocalDateTime now =LocalDateTime.now();
        // 计算两个时间点的差异
        Duration duration = Duration.between(localDateTime, now);

        // 获取差异的小时、分钟、秒
        long hours = duration.toHours();
        long minutes = duration.toMinutes() % 60;
        long seconds = duration.getSeconds() % 60;
        if(hours>24){
            long dayhours=hours;
            hours =hours%24;
            long days = dayhours/24;
            return days+"天"+hours + " 小时 " + minutes + " 分钟 " + seconds + " 秒";
        }

        return  hours + " 小时 " + minutes + " 分钟 " + seconds + " 秒";
    }


    public  static String getFormatDate(Date date){

        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
        return simpleDateFormat.format(date);

    }

    public static String duration(long milliseconds){

        // 将毫秒转换为秒
        long seconds = milliseconds / 1000;
        // 计算分钟
        long minutes = seconds / 60;
        // 计算剩余的秒数
        seconds = seconds % 60;
        return minutes + " 分 " + seconds + " 秒";
    }
}
